package service

import (
	"context"
	"strconv"

	"gitee.com/thewalkers/refactoring-code/model"
	"gitee.com/thewalkers/refactoring-code/model/apperrors"
)

// HourlyClassificationService acts as a struct for injecting an implementation of EmployeeRepository
// for use in service methods
type HourlyClassificationService struct {
	ChangeHourlyTransactionRepository model.ChangeHourlyTransactionRepository
	PaymentClassificationRepository   model.PaymentClassificationRepository
}

// HCConfig will hold repository that will eventually be injected into this
// service layer
type HCConfig struct {
	ChangeHourlyTransactionRepository model.ChangeHourlyTransactionRepository
	PaymentClassificationRepository   model.PaymentClassificationRepository
}

// NewHourlyClassificationService is a factory function for
// initializing a EmployeeService with its repository layer dependencies
func NewHourlyClassificationService(h *HCConfig) model.ChangeHourlyTransactionService {
	return &HourlyClassificationService{
		ChangeHourlyTransactionRepository: h.ChangeHourlyTransactionRepository,
		PaymentClassificationRepository:   h.PaymentClassificationRepository,
	}
}

func (h *HourlyClassificationService) Update(ctx context.Context, id int64, hour *model.HourClassification) error {
	paymentClassification, err := h.PaymentClassificationRepository.FindByEmlpoyeeID(ctx, id)
	if err != nil {
		return apperrors.NewNotFound("paymentClassificationID", strconv.Itoa(int(id)))
	}

	hour.PaymentClassificationID = paymentClassification.ID

	err = h.ChangeHourlyTransactionRepository.Update(ctx, hour)

	return err
}
